package com.jingdun.sport.activity

import com.jingdun.sport.authority.SecurityChecker
import com.jingdun.sport.bean.model.ApiResult
import com.jingdun.sport.data.environment.DataEnvironment
import com.jingdun.sport.data.environment.intGeneratedKey
import com.jingdun.sport.sporttype.SportConfig
import com.jingdun.sport.sporttype.SportTypes
import com.losttemple.sql.language.operator.*
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*

class Activity {
    var type: Int? = null
    var fixedType: Int = 0
    var fixedMeasure: Int = 0
}

@RestController
@RequestMapping("/sport/activities/")
class ActivityController {
    @Autowired
    private lateinit var sql: DataEnvironment

    @Autowired
    private lateinit var securityChecker: SecurityChecker
    
    @PostMapping
    fun addActivity(@RequestBody activity: Activity): ResponseEntity<Any> {
        val apiResult: ApiResult<*> = ApiResult<Any?>()
        //val type = activity.type ?: return ResponseEntity.notFound().build()
        val type = activity.type ?: return ResponseEntity.ok(apiResult)
        sql.run {
            val config = from { SportTypes(it) }
                    .where { id eq type }
                    .use { need(it.currentConfig) }
                    .select { it.currentConfig() }
                    .first() ?: return ResponseEntity.ok(apiResult)
            apiResult.data = db { SportActivities(it) }
                    .insert {
                        it.config(config)
                        it.fixedType(activity.fixedType)
                        it.fixedMeasure(activity.fixedMeasure)
                    }.ret {
                        intGeneratedKey()
                    }()!!
        }
        //return ResponseEntity.ok("succeed")
        return ResponseEntity.ok(apiResult)
    }

    @GetMapping("/{activityId}")
    fun activityDetail(@PathVariable("activityId") activityId: Int): ResponseEntity<Any> {
        val apiResult: ApiResult<*> = ApiResult<Any?>()
        val user = securityChecker.user
        val resultActivity = sql.run {
            (from { SportActivities(it) } innerJoin from { SportConfig(it) })
                    .on { activity, config -> activity.config eq config.id }
                    .select { activity, config ->
                        object {
                            val activity = activity
                            val config = config
                        }
                    }.where { activity.id eq activityId }
                    .use {
                        need(it.activity.fixedType)
                        need(it.activity.fixedMeasure)
                        need(it.config.supportCounting)
                        need(it.config.supportTiming)
                        need(it.config.description)
                        need(it.config.mainLogo)
                        need(it.config.trainVideo)
                        need(it.config.introduceVideo)
                    }.select {
                        object {
                            val fixedType = it.activity.fixedType()
                            val fixedMeasure = it.activity.fixedMeasure()
                            val supportCounting = it.config.supportCounting()
                            val supportTiming = it.config.supportTiming()
                            val description = it.config.description()
                            val mainLogo = it.config.mainLogo()
                            val trainVideo = it.config.trainVideo()
                            val introduceVideo = it.config.introduceVideo()
                        }
                    }.firstOrNull()
        } ?: return ResponseEntity.ok(apiResult)
        apiResult.data = resultActivity
        return ResponseEntity.ok(apiResult)
    }
}